var window = floaty.window(
    <frame>
        <card w="200" h="*" cardCornerRadius="5dp" cardElevation="0dp" gravity="center" layout_gravity="center">
            <vertical w="*" h="*" bg="#66000000">
                <text id="title" textSize="16" h="40" textColor="#ffffff" textStyle="bold" text="阅读助手"gravity="center"/>
                <View w="*" h="1" bg="#E0E0E0"/>

                <horizontal h="40" w="*" margin="8 0 8 0">
                    <text text="开始：" textColor="#E0E0E0" gravity="center_vertical" textSize="14" w="0" layout_weight="1"/>
                    <input id="start_time" textSize="14sp" textColor="#E0E0E0" inputType="number" focusable="true" w="0" h="*"  text="2000" hint="0" layout_weight="1" />
                    <text textSize="14sp"  text="毫秒" textColor="#E0E0E0" gravity="center_vertical"  w="30" h="*"/>
                </horizontal>
                <horizontal h="40" w="*" margin="8 0 8 0">
                    <text text="结束：" textColor="#E0E0E0" gravity="center_vertical" textSize="14" w="0" layout_weight="1"/>
                    <input id="end_time" textSize="14sp" textColor="#E0E0E0" inputType="number" focusable="true" w="0" h="*"  text="3600" hint="0" layout_weight="1" />
                    <text textSize="14sp"  text="毫秒" textColor="#E0E0E0" gravity="center_vertical"  w="30" h="*"/>
                </horizontal>

                <View w="*" h="1" bg="#E0E0E0" margin="0 8 0 8"/>
                <horizontal h="30" w="*" layout_gravity="center" gravity="center" margin="0 0 0 8">
                    <card w="60" h="30" cardCornerRadius="3dp" cardElevation="0dp" layout_gravity="center" marginRight="8">
                        <text id="start" text="开启" h="*" w="*" textColor="#FFFFFF"  gravity="center" textSize="13sp" bg="#FF1CBBB4" />
                    </card>
                    
                    <card w="60" h="30" cardCornerRadius="3dp" cardElevation="0dp" layout_gravity="center" marginLeft="8">
                        <text id="stopUi" text="关闭" h="*" w="*" textColor="#FFFFFF" gravity="center" textSize="13sp" bg="#ff0000" />
                    </card>
                </horizontal>
            </vertical>
        </card>
    </frame>
);


window.setPosition(200, 200);
window.setSize(-2,-2);
window.exitOnClose();

setInterval(() => { }, 1000);

setTimeout(openConsole,500);
//判断当前线程是否启动
var start_status = true;
var start_slide = true;

var num_ = 1;

window.title.on("click", () => {
    //开启位置调整
    window.setAdjustEnabled(!window.isAdjustEnabled());
});

window.start_time.on("touch_down", () => {
    window.requestFocus();
    window.start_time.requestFocus();
});

window.end_time.on("touch_down", () => {
    window.requestFocus();
    window.end_time.requestFocus();
});

//退出
window.stopUi.on("click", () => {
    console.hide();
    window.close();
    floaty.closeAll();
    threads.shutDownAll();
    exit();
});

window.start.click(()=>{
    var s = parseInt(window.start_time.getText().toString());
    var e = parseInt(window.end_time.getText().toString());
    window.disableFocus();
    if(e<=s){
        toast("结束时间不能小于开始时间！");
        return;
    }
    if(e-s<=1500){
        toast("开始时间与结束时间的时间差不能小于1.5秒！");
        return;
    }
    if(start_status){//开启
        // setBg("https://gitee.com/wupk/auto.test/raw/master/img/end.png");
        window.start.setText("停止");
        if(start_slide){
            threads.start(run);
        }else{
            start_slide = true;
        }
    }else{//停止
        // setBg("https://gitee.com/wupk/auto.test/raw/master/img/start.png");
        setTimeout(stop, 500);
    }
});

/**
 * 设置网络图片地址
 * @param {图片加载地址}} url 
 */
function setBg(url){
    new com.stardust.autojs.core.ui.inflater.util.Drawables().loadInto(window.fab_img, 
        android.net.Uri.parse(url));
}


function stop(){
    window.start.setText("开启");
    start_slide = false;
    start_status = true;
    console.log("程序暂停执行，等待中...");
}

function run(){
    start_status = false;
    // 一直保持屏幕常亮
    device.keepScreenOn();
    setInterval(function(){
        if(start_slide){
            var duration=random(350,500);
            huadong(duration);
        }
    },1000);
}

function openConsole(){
    //显示控制台
    var middle = device.width / 2 - 400;
    console.setSize(800, 800);
    console.setPosition(middle, 80);
    console.show();
    console.setTitle("阅读助手");
    console.log("请打开要滑动的App");
    console.log("本次滑动方向: 右->左");
}

/**
 * 
 * @param {*滑动时间} duration 
 */
function huadong(duration){
    //设备宽高度根据手机尺寸可调节
    var width_2=200;
    //随机等待时间random（最小值，最大值）根据需要更改数值（单位秒）
    var s = parseInt(window.start_time.getText().toString());
    var e = parseInt(window.end_time.getText().toString());
    var time_random=random(s,e);
    swipe(device.width-random(50,120),device.height/2 ,random(50,120),device.height/2,duration);
    console.info("总滑动次数: "+num_+" 次 ~"+time_random);
    num_++;
    sleep(time_random);
}